﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SQLite;
using System.Threading;
using System.Windows.Forms;
using Eagleshoes.PackingBarcode.Properties;

namespace Eagleshoes.PackingBarcode.PackagingScan.Offline
{
    public partial class UploadOfflineDataForm : Form
    {
        private readonly SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0}", Program.DataPath + @"\packagingdata.db3"));

        private bool canClose;

        public UploadOfflineDataForm()
        {
            InitializeComponent();
        }

        #region Form Events

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            var t = new Thread(() =>
                {
                    Invoke(new Action(() =>
                        {
                            canClose = false;
                            btnUpload.Enabled = false;
                            lblMsg.Text = "开始读取离线扫描数据";
                            pb1.Value = 0;
                        }));

                    List<MyPackagingCarton> pcs = MyPackagingCarton.GetScanedAndNonUploadPackagingCartonList(connection);

                    Invoke(new Action(() =>
                        {
                            lblMsg.Text = "读取完毕，开始上传";
                            pb1.Value = 0;
                            pb1.Maximum = pcs.Count;
                        }));
                    try
                    {
                        foreach (MyPackagingCarton pc in pcs)
                        {
                            var pcs1 = new List<MyPackagingCarton>();
                            pcs1.Add(pc);
                            if (MyPackagingCarton.SaveScanedPackagingCartonOnline(pcs1))
                            {
                                MyPackagingCarton.MarkToUploaded(connection, pc.CartonBarcode, true);
                                Invoke(new Action(() =>
                                    {
                                        pb1.Value++;
                                        lblMsg.Text = string.Format("正在上传。已上传{0}", pb1.Value);
                                    }));
                            }
                            Thread.Sleep(100);
                        }

                        Invoke(new Action(() =>
                            {
                                lblMsg.Text = "上传成功";
                                Win32.sndPlaySound(Resources.写入成功, Win32.SND_SYNC | Win32.SND_MEMORY);
                            }));
                    }
                    catch
                    {
                        Invoke(new Action(() =>
                            {
                                lblMsg.Text = "发生错误";
                                Win32.sndPlaySound(Resources.通讯错误, Win32.SND_SYNC | Win32.SND_MEMORY);
                            }));
                        return;
                    }
                    finally
                    {
                        canClose = true;
                        Invoke(new Action(() => { btnUpload.Enabled = true; }));
                    }
                });
            t.Start();
        }

        private void UploadOfflineDataForm_Closing(object sender, CancelEventArgs e)
        {
            e.Cancel = !canClose;
        }

        private void UploadOfflineDataForm_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    if (btnUpload.Enabled)
                    {
                        Close();
                    }
                    break;
            }
        }

        #endregion
    }
}